package la.iok.hzsvn.lewin.movie.controller;

import la.iok.hzsvn.lewin.movie.core.RoleEnum;
import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.exception.ErrorCode;
import la.iok.hzsvn.lewin.movie.model.ObsSignedUrl;
import la.iok.hzsvn.lewin.movie.model.SignUrlParam;
import la.iok.hzsvn.lewin.movie.obs.AccessKey;
import la.iok.hzsvn.lewin.movie.obs.ObsService;
import la.iok.hzsvn.lewin.movie.service.KeyValueService;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("obs")
public class ObsController {
    private static final String[] OP_ARR = new String[]{"read","write"};
    private final ObsService obsService;
    private final KeyValueService keyValueService;

    public ObsController(ObsService obsService,
                         KeyValueService keyValueService) {
        this.obsService = obsService;
        this.keyValueService = keyValueService;
    }

    @RequestMapping("readAccessKey")
    public SingleDataResponse<AccessKey> accessKey(@RequestHeader Token token){
        ErrorCode.DEVICE_NOT_EXIST.assertNotNull(token.getDeviceId(),token.getDeviceId());
        if(keyValueService.obsReadEnabled()){
            return new SingleDataResponse<>(obsService.getAccessKey(false));
        }else{
            return new SingleDataResponse<>((AccessKey)null);
        }
    }

    @RequestMapping("writeAccessKey")
    public SingleDataResponse<AccessKey> writeAccessKey(@RequestHeader Token token){
        ErrorCode.PROPERTY_CHECK_ERROR.assertTrue(token.hasAnyAuthority(RoleEnum.Admin,RoleEnum.Author),"权限","不具备写文件权限");
        if(keyValueService.obsWriteEnabled()){
            return new SingleDataResponse<>(obsService.getAccessKey(true));
        }else{
            return new SingleDataResponse<>((AccessKey)null);
        }
    }

    /**
     * 权限开关
     * @param op 读或者写,值分别为read/write
     * @param status 开关状态,true开启,false关闭
     * @return 操作结果
     */
    @PutMapping("switch/{op}/{status}")
    public OperatorResponse obsAccessSwitch(@RequestHeader Token token, @PathVariable String op,@PathVariable boolean status){
        ErrorCode.INVALID_OPERATION.assertAnyOf(op,OP_ARR);
        if(OP_ARR[1].equals(op)){
            if(status){
                keyValueService.enableObsWrite(token);
            }else{
                keyValueService.cancelObsWrite(token);
            }
        }else {
            if(status){
                keyValueService.enableObsRead(token);
            }else{
                keyValueService.cancelObsRead(token);
            }
        }
        return OperatorResponses.success();
    }

    /**
     * 分段上传时签名初始化分段,分段上传，合并分段及取消分段的url
     * @param param 请求参数
     * @return 签名的url
     */
    @PostMapping("signedUrl")
    public SingleDataResponse<ObsSignedUrl> signPartUrl(@RequestBody SignUrlParam param){
        return new SingleDataResponse<>(obsService.signUrl(param));
    }
}
